package com.yztc.orangeentertainment.ui.main.findfragment.findadapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.findbean.FindMoreDataListBean;
import com.yztc.orangeentertainment.ui.main.findfragment.findmain.FindMoreActivity;
import com.yztc.orangeentertainment.ui.main.findfragment.findutils.CustomShapeTransformation;
import com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.StarItemActivity;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/13.
 */
public class FindMoreRlvAdapter extends RecyclerView.Adapter<FindMoreRlvAdapter.MoreViewHolder> {
    private List<FindMoreDataListBean> list;
    private Context context;
    boolean flag = false;

    public FindMoreRlvAdapter(Context context, List<FindMoreDataListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MoreViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MoreViewHolder holder = new MoreViewHolder(LayoutInflater.from(context).inflate(R.layout
                .findmore_rlv, null));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MoreViewHolder holder, final int position) {
        holder.qiandaoIv.setImageResource(R.mipmap.btg_icon_assistivebutton_submit);
        if (position == 0) {
            holder.rlFindmore.removeAllViewsInLayout();
            Glide.with(context).load(list.get(position + 1).getPortrait()).bitmapTransform(new
                    CustomShapeTransformation(context, R.mipmap
                    .ic_discover_staravatar_bg)).into
                    (FindMoreActivity.moreIv1);
            FindMoreActivity.showTvname1.setText(list.get(position + 1).getName());
            FindMoreActivity.showTveng1.setText(list.get(position + 1).getEngname());
            FindMoreActivity.findmoreIv1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StarItemActivity.class);
                    intent.putExtra("sid", list.get(position + 1).getId() + "");
                    context.startActivity(intent);
                }
            });

            FindMoreActivity.findmoreStar1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (flag == list.get(position + 1).isIs_praised()) {
                        FindMoreActivity.findmoreSatrtv1.setText(list.get(position + 1).getPraise
                                () + 1 + "");
                        list.get(position + 1).setIs_praised(true);
                        FindMoreActivity.findmoreStariv1.setImageResource(R.mipmap
                                .btg_icon_assistivebutton_submit_pressed);
                    } else {
                        Toast.makeText(context, "你已支持过了", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else if (position == 1) {
            holder.rlFindmore.removeAllViewsInLayout();
            Glide.with(context).load(list.get(position - 1).getPortrait()).bitmapTransform(new
                    CustomShapeTransformation(context, R.mipmap
                    .ic_discover_staravatar_bg)).into
                    (FindMoreActivity.moreIv2);
            FindMoreActivity.showTvname2.setText(list.get(position - 1).getName());
            FindMoreActivity.showTveng2.setText(list.get(position - 1).getEngname());
            FindMoreActivity.findmoreIv2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StarItemActivity.class);
                    intent.putExtra("sid", list.get(position - 1).getId() + "");
                    context.startActivity(intent);
                }
            });

            FindMoreActivity.findmoreStar2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (flag == list.get(position - 1).isIs_praised()) {
                        FindMoreActivity.findmoreSatrtv2.setText(list.get(position - 1).getPraise
                                () + 1 + "");
                        list.get(position - 1).setIs_praised(true);
                        FindMoreActivity.findmoreStariv2.setImageResource(R.mipmap
                                .btg_icon_assistivebutton_submit_pressed);
                    } else {
                        Toast.makeText(context, "你已支持过了", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else if (position == 2) {
            holder.rlFindmore.removeAllViewsInLayout();
            Glide.with(context).load(list.get(position).getPortrait()).bitmapTransform(new
                    CustomShapeTransformation(context, R.mipmap
                    .ic_discover_staravatar_bg)).into
                    (FindMoreActivity.moreIv3);
            FindMoreActivity.showTvname3.setText(list.get(position).getName());
            FindMoreActivity.showTveng3.setText(list.get(position).getEngname());
            FindMoreActivity.findmoreIv3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StarItemActivity.class);
                    intent.putExtra("sid", list.get(position).getId() + "");
                    context.startActivity(intent);
                }
            });

            FindMoreActivity.findmoreStar3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (flag == list.get(position).isIs_praised()) {
                        FindMoreActivity.findmoreSatrtv3.setText(list.get(position).getPraise
                                () + 1 + "");
                        list.get(position).setIs_praised(true);
                        FindMoreActivity.findmoreStariv3.setImageResource(R.mipmap
                                .btg_icon_assistivebutton_submit_pressed);
                    } else {
                        Toast.makeText(context, "你已支持过了", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            holder.findmoreNum.setText(position + 1 + "");
            Glide.with(context).load(list.get(position).getPortrait()).bitmapTransform(new
                    CustomShapeTransformation(context, R.mipmap
                    .abc_btn_switch_to_on_mtrl)).into
                    (holder.findmoreIv);
            holder.findmoreName.setText(list.get(position).getName());
            holder.qiandaoNum.setText(list.get(position).getPraise() + "");
            holder.findmoreQiandao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (flag == list.get(position).isIs_praised()) {
                        holder.qiandaoNum.setText(list.get(position).getPraise() + 1 + "");
                        list.get(position).setIs_praised(true);
                        holder.qiandaoIv.setImageResource(R.mipmap
                                .btg_icon_assistivebutton_submit_pressed);
                    } else {
                        Toast.makeText(context, "你已支持过了", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            holder.findmoreIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StarItemActivity.class);
                    intent.putExtra("sid", list.get(position).getId() + "");
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MoreViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.findmore_num)
        TextView findmoreNum;
        @InjectView(R.id.findmore_iv)
        ImageView findmoreIv;
        @InjectView(R.id.findmore_name)
        TextView findmoreName;
        @InjectView(R.id.qiandao_iv)
        ImageView qiandaoIv;
        @InjectView(R.id.qiandao_num)
        TextView qiandaoNum;
        @InjectView(R.id.findmore_qiandao)
        RelativeLayout findmoreQiandao;
        @InjectView(R.id.rl_findmore)
        RelativeLayout rlFindmore;

        public MoreViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }
}
